﻿using System;
using System.Collections.Generic;

namespace ConsoleApp2
{
    class Program
    {
        static void Main(string[] args)
        {
            //模拟数据
            var logs = new List<Log>{
              new Log{ LoginTime=2, LogoutTime=5 },
              new Log{ LoginTime=3, LogoutTime=6 },
              new Log{ LoginTime=3, LogoutTime=4 },
              new Log{ LoginTime=4, LogoutTime=8 },
              new Log{ LoginTime=4, LogoutTime=9 },
              new Log{ LoginTime=4, LogoutTime=10 },
              new Log{ LoginTime=3, LogoutTime=4 },
              new Log{ LoginTime=4, LogoutTime=8 },
              new Log{ LoginTime=5, LogoutTime=6 },
          };

            #region 获取每个小时在线人数

            //定义一个数组盛放每个小时的在线人数
            int[] logHigh = new int[24];
            int time = 0;
            while (time < 24)
            {
                logHigh[time] = 0;
                foreach (var log in logs)
                {
                    if (time >= log.LoginTime && time < log.LogoutTime)
                    {
                        logHigh[time]++;
                    }
                }
                time++;
            }

            #endregion
            #region 获取最大在线人数和持续时间

            //获取最大在线人数
            var max = logHigh[0];
            var index = 0;
            for (int j = 1; j < logHigh.Length; j++)
            {
                if (max < logHigh[j])
                {
                    max = logHigh[j];
                    index = j;
                }
            }
            //获取最大在线人数持续时间
            var maxIndex = 0;
            for (var maxI = index + 1; maxI < logHigh.Length; maxI++)
            {
                if (logHigh[maxI] == max)
                {
                    maxIndex = maxI;
                }
                else
                {
                    continue;
                }

            }

            #endregion
            Console.WriteLine("最大在线人数是：" + max);
            Console.WriteLine($"起始时间是：{index}，结束时间是：{maxIndex + 1}，持续时间：{maxIndex + 1 - index}h");

            for (var i = 0; i < 24; i++)
            {
                Console.WriteLine($"时间在 {i} 到 {i + 1} 点之间在线人数是：{logHigh[i].ToString()}");
            }
            Console.ReadKey();
        }
    }
}
